package com.tifa.vote.dao.impl;

import com.tifa.vote.bean.User;
import com.tifa.vote.dao.UserDao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @Classname UserDaoImpl
 * @Description
 * @Date 2022/6/15 12:48
 * @Created by hengyuan
 */
@Repository
public class UserDaoImpl implements UserDao {

  @Autowired
  private NamedParameterJdbcTemplate jdbcTemplate;

  @Override
  public List<User> getUsers() {
    String sql = "select id,name,password from user";
    RowMapper<User> rm = BeanPropertyRowMapper.newInstance(User.class);
    List<User> query = jdbcTemplate.query(sql, rm);
    return query;
  }

  @Override
  public boolean deleteUser(Integer id) {
    String sql = "delete from user where id =:id";
    Map<String, Object> paramMap = new HashMap<>();
    paramMap.put("id", id);
    jdbcTemplate.update(sql, paramMap);
    return true;
  }

  @Override
  public boolean addUser(User user) {
    String sql = "insert into user(name,password) values(:name,:password)";
    Map<String, Object> paramMap = new HashMap<>();
    paramMap.put("name", user.getName());
    paramMap.put("password", user.getPassword());
    jdbcTemplate.update(sql, paramMap);
    return true;
  }
}
